package com.k.base.jdk.thread.demo1;

import java.util.concurrent.TimeUnit;

/**
 * 守护线程
 * t1.setDaemon(true) 子线程执行可能不完整  输出:test0 test1 test2 ===== main end
 * t1.setDaemon(false)等待子线程执行       输出：test0 test1 test2 ===== main end test3 test4 test5 test6 test7 test8 test9
 */
public class ThreadTest {
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> test1());
        // true or false different
        t1.setDaemon(true);
        t1.start();
        TimeUnit.MILLISECONDS.sleep(3000);
        System.out.println("===== main end");
    }

    public static void test1() {
        for (int i = 0; i < 10; i++) {
            try {
                System.out.println("test" + i);
                TimeUnit.MILLISECONDS.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
